import com.sun.source.tree.Tree;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2024-11-29
 * Time: 22:11
 */
public class HashBucket {
    static class Node {
        int key;
        int value;
        Node next;

        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }
    public Node[] tables = new Node[10];
    public int usedSize;
    public static final double DEFAULT_LOAD_FACTOR = 0.75F;
    public void push(int key,int value) {
        int index = key % tables.length;
        Node cur = tables[index];
        while(cur != null) {
            if(cur.key == key) {
                cur.value = value;
                return;
            }
            cur = cur.next;
        }
        Node node = new Node(key,value);
        node.next = tables[index];
        tables[index] = node;
        usedSize++;
        if(doLoadFactor() >= DEFAULT_LOAD_FACTOR) {
            resize();
        }
    }

    private void resize() {
        Node[] newTables = new Node[2*tables.length];
        for (int i = 0; i < tables.length; i++) {
            Node cur = tables[i];
            while(cur != null) {
                int index = cur.key % newTables.length;
                Node curN = cur.next;
                cur.next = newTables[index];
                newTables[index] = cur;
                cur = curN;
            }
        }
        tables = newTables;
    }

    private double doLoadFactor() {
        return usedSize*1.0 / tables.length;
    }

    public int getVal(int key) {
        int index = key % tables.length;
        Node cur = tables[index];
        while(cur != null) {
            if(cur.key == key) {
                return cur.value;
            }
            cur = cur.next;
        }
        return -1;
    }
}
